﻿@model vITs.Site.Models.DataModels.Assignment
@using vITs.Site.Models.DataModels
@using vITs.Site.Models
@{
    Layout = null;
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + ViewBag.Filename);
    //Content-Disposition is defined in RFC-2183
    var totalSum = 0.0f + -this.Model.Advance;
}
        <h2>@this.ViewBag.Title</h2>
        <table>
            <tr>
                <td> Konsult: @this.Model.Consultant.GivenName @this.Model.Consultant.Surname</td>
            </tr>
            <tr>
                <td>Handläggare: @this.Model.Handler.GivenName @this.Model.Handler.Surname</td>
            </tr>
            <tr>
                <td>Tidsperiod: @this.Model.StartDate.ToString("yyyy-MM-dd") &mdash; @this.Model.EndDate.ToString("yyyy-MM-dd")</td>
            </tr>
            <tr>
                <td> Förskott: @this.Model.Advance SEK</td>
            </tr>
            <tr>
                <td>Status: @this.Model.GetStatus().GetDisplayName()</td>
            </tr>
            @if (this.Model.Breaks.Count > 0)
            {
            <tr>
                <td><h3>Uppehåll</h3></td>
            </tr>
            foreach (Break brk in this.Model.Breaks.OrderBy(b => b.StartDate))
	        {
                <tr>
                    <td>@brk.StartDate.ToString("yyyy-MM-dd") &mdash; @brk.EndDate.ToString("yyyy-MM-dd")</td>
                </tr>
	        }
    }
    @if (this.Model.Trips.Count > 0)
    {
        var tripSum = 0.0d;
        
            <tr>
                <td><h3>Resor</h3></td>
            </tr>
                foreach (Trip trip in this.Model.Trips.OrderBy(t => t.StartDate))
                {
                        <tr>
                            <td>@trip.StartLocation @trip.EndLocation</td>
                        </tr>
                        <tr>
                            <td>@trip.StartDate.ToString("yyyy-MM-dd") &ndash; @trip.EndDate.ToString("yyyy-MM-dd")</td>
                        </tr>
                        <tr>
                            
                            
                        @if (trip.GetTransportationMode() == TransportationMode.Car)
                        {
                            var cost = trip.TransportationCost * 18.50d;
                            tripSum += cost;
                            <td>@trip.GetTransportationMode().GetDisplayName(), @trip.TransportationCost Mil, (@cost SEK)</td>
                        }
                        else
                        {
                            tripSum += trip.TransportationCost;
                             <td>@trip.GetTransportationMode().GetDisplayName(), @trip.TransportationCost SEK</td>
                        }
                        </tr> 
                }
              <tr>
                <td>Total kostnad: @tripSum SEK</td>
              </tr>  
        
        totalSum += tripSum;
    }
    @if (this.Model.PerDiems.Count > 0)
    {
        var perDiemSpecifications = new VitsDB().PerDiemSpecifications;
        var perDiemCost = 0d;
        
                <tr>
                    <td><h3>Traktamenten</h3></td>
                </tr>
                foreach (PerDiem pd in this.Model.PerDiems.OrderBy(p => p.Date))
	            {
                    var specification = perDiemSpecifications.SingleOrDefault(s => s.Country == pd.Country && s.Year == pd.Date.Year);
                    var cost = 0.0d;
                    cost += specification.Sum;
                    <tr>
                        <td>@pd.Date.ToString("yyyy-MM-dd") @pd.Country</td>
                    </tr>
                        if (pd.Breakfast)
                        {
                            cost -= 44;
                            <tr>
                                <td>Frukost</td>
                            </tr>
                        }
                        if (pd.Lunch)
                        {
                            cost -= 77;
                            <tr>
                                <td>Lunch</td>
                            </tr>
                        }
                        if (pd.Dinner)
                        {
                            cost -= 77;
                            <tr>
                                <td>Middag</td>
                            </tr>
                        }
                        
                        <tr>
                            <td>@cost SEK</td>
                        </tr>
                    
                    perDiemCost += cost;
                }
                <tr>
                    <td>Total Kostnad: @perDiemCost SEK</td>
                </tr>
        
        totalSum += perDiemCost;
    }
    @if (this.Model.Expenses.Count > 0)
    {
        var expensesCost = 0d;
        
            <tr>
                <td><h3>Utgifter</h3></td>
            </tr>
                foreach (Expense exp in this.Model.Expenses.OrderBy(e => e.Date))
                {
                        <tr>
                            <td>@exp.Name &ndash; @exp.Sum @exp.Currency</td>
                        </tr>
                        if (exp.Tax > 0)
                        {
                            var netto = @exp.Sum - @exp.Tax;
                            
                            <tr>
                                <td>@netto @exp.Currency</td>
                            </tr>
                        }
                        <tr>
                            <td>@exp.Date.ToString("yyyy-MM-dd") &nbsp;</td>
                        </tr>
                        if (exp.Tax > 0)
                        {
                            <tr>
                                <td>Kvitto finns</td>
                            </tr>
                            
                        }
                    
                    expensesCost += (exp.Sum * exp.ExchangeRate);
                }
            <tr>
                <td>Total Kostnad @expensesCost SEK</td>
            </tr>
        totalSum += expensesCost;
    }
    <tr>
        <td>Total kostnad för uppdraget: @totalSum SEK</td>
    </tr>
    </table>

